<?php
// +----------------------------------------------------------------------
// |MCPHP[基于ThinkPHP6.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://www.mingchangruanjian.com
// +----------------------------------------------------------------------
// | Author jokerfei [ jokerfei377@gmail.com ]
// +----------------------------------------------------------------------
// | 铭畅软件 技术部 出品
// +----------------------------------------------------------------------
namespace addons\Alipay\controller;

/**
 * Description of Base
 * @author jokerfei<215628355@qq.com>
 */
class Base
{

    public $config;

    public function __construct(\think\Request $request = null)
    {
        $this->config = addons_config('Alipay');
        if (isset($this->config['ali_public_key_path'])) {
            $fileName = md5($this->config['ali_public_key_path']);
            $path     = __DIR__ . '/../cert/' . $fileName . '.pem';
            if (!file_exists($path)) {
                try {
                    file_put_contents($path, $this->config['ali_public_key_path']);
                } catch (\Exception $e) {
                    throw new \Exception("公钥文件写入失败");
                }
            }
            $this->config['ali_public_key_path'] = $path;
        }

        if (isset($this->config['private_key_app_path'])) {
            $fileName = md5($this->config['private_key_app_path']);
            $path     = __DIR__ . '/../cert/' . $fileName . '.pem';
            if (!file_exists($path)) {
                try {
                    file_put_contents($path, $this->config['private_key_app_path']);
                } catch (\Exception $e) {
                    throw new \Exception("私钥文件写入失败");
                }
            }
            $this->config['private_key_app_path'] = $path;
        }
    }

    //直接载入Rrequest
    public function loadRrequest($name)
    {
        $names = explode(".", $name);
        foreach ($names as &$val) {
            $val = ucfirst($val);
        }
        $name = implode('', $names);
        require_once __DIR__ . '/../sdk/Aop/request/' . $name . '.php';
    }

}
